package com.bishe.tax.controller.sys;

import com.bishe.tax.entity.SysUser;
import com.bishe.tax.entity.TaxNews;
import com.bishe.tax.service.TaxNewsService;
import com.bishe.tax.utils.AjaxResult;
import com.bishe.tax.utils.TableDataInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * 新闻 信息操作处理
 *
 * @author ruoyi
 * @date 2019-06-23
 */
@Controller
@RequestMapping("/system/news")
public class TaxNewsController {
    private String prefix = "news";

    @Autowired
    private TaxNewsService newsService;

    @GetMapping("list")
    public String news() {
        return prefix + "/list";
    }

    /**
     * 查询新闻列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TaxNews news, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                              @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<TaxNews> list = newsService.selectNewsList(news);
        return TableDataInfo.returnDataTable(new PageInfo<>(list).getTotal(), list);
    }


    /**
     * 新增新闻
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存新闻
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TaxNews news, HttpSession session) {
        SysUser user = (SysUser) session.getAttribute("sysUser");
        news.setCreateTime(new Date());
        news.setCreateBy(user.getUserName());
        return AjaxResult.success(newsService.insertNews(news));
    }

    /**
     * 修改新闻
     */
    @GetMapping("/edit/{newsId}")
    public String edit(@PathVariable("newsId") Integer newsId, ModelMap mmap) {
        TaxNews news = newsService.selectNewsById(newsId);
        mmap.put("news", news);
        return prefix + "/edit";
    }

    /**
     * 修改保存新闻
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TaxNews news) {
        return AjaxResult.success(newsService.updateNews(news));
    }

    /**
     * 删除新闻
     */
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return AjaxResult.success(newsService.deleteNewsByIds(ids));
    }

}
